﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using SqlCeViewer;
using SqlCeViewer.Base;

namespace SqlCeViewer.DynaControls
{
    public partial class GetDbTypeControl : BaseSubForm
    {
        public List<int> NotInList = new List<int>();
        public delegate void Del_AfterSelectedValue(string a);
        public Del_AfterSelectedValue AfterSelectedValue = null;
        public GetDbTypeControl()
        {
            InitializeComponent();

            this.Text = "";

            FormBorderStyle = FormBorderStyle.None;
            Button but = new Button();
            but.Click += delegate { Dispose(); };
            CancelButton = but;

            NotInList.Add(24);
            NotInList.Add(26);
            NotInList.Add(27);
            NotInList.Add(28);
            listBox1.DoubleClick += new EventHandler(listBox1_DoubleClick);
            listBox1.KeyDown += new KeyEventHandler(listBox1_KeyDown);
            //string item = string.Empty;
            //This SqlDbType is used for standard sql db type
            //sqlce need specical
            //for (int i = 0; i < 35; i++)
            //{
            //    if (!NotInList.Contains(i))
            //    {
            //       // this.listBox1.Items.Add(((SqlDbType)i).ToString());
            //    }
            //}

            DataSet dbTypeDs = GlobalInfo.MainEngineer.GetSupportedDbType();
            foreach (DataRow item in dbTypeDs.Tables[0].Rows)
            {
                this.listBox1.Items.Add(item[0].ToString());
            }

            Load += new EventHandler(GetDbTypeControl_Load);
        }

        void listBox1_DoubleClick(object sender, EventArgs e)
        {
            DoSelect();
        }

        void listBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                DoSelect();
            }
        }

        private void DoSelect()
        {
            string result = string.Empty;

            if (this.listBox1.SelectedItem != null)
            {
                result = this.listBox1.SelectedItem.ToString();
                if (AfterSelectedValue != null)
                {
                    AfterSelectedValue(result);

                }
                Dispose();
            }
        }

        void GetDbTypeControl_Load(object sender, EventArgs e)
        {
            this.listBox1.Focus();
            this.listBox1.SetSelected(0, true);
        }
    }
}
